package com.ylbh.modules.sign.service.impl;

import com.ylbh.common.pojo.sign.dto.SignPrizeDTO;
import com.ylbh.common.pojo.sign.dto.SignTurntableDTO;
import com.ylbh.common.pojo.sign.vo.ShoppingSigninPrizeVO;
import com.ylbh.common.utils.OSSClientUtil;
import com.ylbh.modules.activity.mapper.ActivityGoodsMapper;
import com.ylbh.modules.activity.service.ActivityGoodsService;
import com.ylbh.modules.shoppingcoupon.mapper.ShoppingCouponMapper;
import com.ylbh.modules.sign.entity.ShoppingSigninPrize;
import com.ylbh.modules.sign.mapper.ShoppingSignPrizeMapper;
import com.ylbh.modules.sign.service.ShoppingSignPrizeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wjc
 */
@Service
@Transactional
public class ShoppingSignPrizeServiceImpl implements ShoppingSignPrizeService{

    @Autowired
    private ShoppingSignPrizeMapper shoppingSignPrizeMapper;
    @Autowired
    private ActivityGoodsMapper activityGoodsMapper;
    @Autowired
    private ShoppingCouponMapper shoppingCouponMapper;

    @Override
    public String uploadPrizeImg(MultipartFile file,  HttpServletRequest request) {
        OSSClientUtil ossClient = null;
        String path = request.getParameter("path");
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadImg2Oss(file,path);//上传文件到服务器
            if("fail".equals(imgUrl)){
                return "上传失败";
            }
            return "http://image.yilianbaihui.cn/consumer/"+ path + "/" + imgUrl;
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return "上传失败";
        }
    }

    @Override
    public ShoppingSigninPrizeVO getPrizeInfoById(Integer id) {
        ShoppingSigninPrizeVO shoppingSigninPrizeVO = shoppingSignPrizeMapper.selectPrizeInfoById(id);
        if(shoppingSigninPrizeVO != null && shoppingSigninPrizeVO.getPrizeType() == 3) {
            shoppingSigninPrizeVO.setT(activityGoodsMapper.getActivityGoodsById(Integer.parseInt(shoppingSigninPrizeVO.getPrizeRelation())));
        } else if (shoppingSigninPrizeVO != null && shoppingSigninPrizeVO.getPrizeType() == 2) {
            shoppingSigninPrizeVO.setT(shoppingCouponMapper.selectCouponInfoById(shoppingSigninPrizeVO.getPrizeRelation()));
        }
        return shoppingSigninPrizeVO;
    }
    @Override
    public void addPrizeInfo(SignPrizeDTO signPrizeDTO) {
        signPrizeDTO.setPrizeAllowance(signPrizeDTO.getPrizeNumber());
        ShoppingSigninPrize prize = new ShoppingSigninPrize();
        BeanUtils.copyProperties(signPrizeDTO,prize);
        prize.setTurnTableInfoId(String.valueOf(signPrizeDTO.getTurnTableInfoId()));
        shoppingSignPrizeMapper.insertPrizeInfo(prize);
    }

    @Override
    public void modifyPrizeInfoById(SignPrizeDTO signPrizeDTO) {
        // 以排序为1的做判断是不是积分不变的那一条
        if (signPrizeDTO.getSort() != 1) {
            signPrizeDTO.setPrizeAllowance(signPrizeDTO.getPrizeNumber());
            ShoppingSigninPrize prize = new ShoppingSigninPrize();
            BeanUtils.copyProperties(signPrizeDTO,prize);
            prize.setTurnTableInfoId(String.valueOf(signPrizeDTO.getTurnTableInfoId()));
            // 不是1这条先插入新数据
            int index = shoppingSignPrizeMapper.insertPrizeInfo(prize);
            if (index ==1) {
                // 成功后把原来 的记录做逻辑删除
                shoppingSignPrizeMapper.updatePrizeDeleteStatusById(signPrizeDTO.getId());
            } else {
                new RuntimeException("修改数据失败");
            }
        }else {
            // 如果是 1 这条直接做修改
            signPrizeDTO.setPrizeAllowance(signPrizeDTO.getPrizeNumber());
            shoppingSignPrizeMapper.updatePrizeInfoById(signPrizeDTO);
        }
    }

    @Override
    public int getPrizeCountById(Integer id) {
        return shoppingSignPrizeMapper.queryPrizeCountById(id);
    }

}
